home *** CD-ROM | disk | FTP | other *** search
- Path: soap.news.pipex.net!pipex!usenet
- From: m.hendry@dial.pipex.com (Mathew Hendry)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: silly c problem
- Date: Wed, 31 Jan 96 14:29:27
- Organization: Private node.
- Distribution: world
- Message-ID: <19960131.4755A8.D114@an181.du.pipex.com>
- References: <f2c_9601302132@techtol.magic.mb.ca>
- NNTP-Posting-Host: an181.du.pipex.com
- X-Newsreader: TIN [AMIGA 1.3 950726BETA PL0]
-
- The problem here is the use of pointers in printf() - integer arguments
- should _not_ normally be referenced with pointers when calling printf(). If
- you do so, what will be printed, logically enough, is the address at which the
- integer is stored, rather than the value of that integer.
-
- Perhaps you are confused by the different calling conventions of printf() and
- scanf() - it is safe to pass by value to printf(), since the value does not
- need to be changed by printf(). scanf(), on the other hand, will alter the
- value and so must be passed a _pointer_ to that value.
-
- Tahir Khawaja (tahir.khawaja@techtol.magic.mb.ca) wrote:
- : #include <stdio.h>
- :
- : main()
- : {
- : int num1, num2, res, ope, err;
- :
- : printf("Please input two number!\n");
- : scanf("%d%d", &num1, &num2);
- : printf("You entered %d and %d.\n\n", &num1, &num2);
-
- printf("You entered %d and %d.\n\n", num1, num2);
-
- : printf("And now the code for the operation!\n");
- : printf("1=Add, 2=Subtract, 3=Multiply, 4=Divide\n");
- : scanf("%d", &ope);
- : printf("You entered %d.\n\n", &ope);
-
- printf("You entered %d.\n\n", ope);
-
- : err = 1;
- : if(ope == 1)
- : {
- : res = num1 + num2;
- : err = 0;
- : }
- : if(ope == 2)
- : {
- : res = num1 - num2;
- : err = 0;
- : }
- : if(ope == 3)
- : {
- : res = num1 * num2;
- : err = 0;
- : }
- : if(ope == 4)
- : {
- : res = num1 / num2;
- : err = 0;
- : }
- : if(err == 1)
- : printf("Wrong Code! Input only number 1 - 4!\n");
- : else
- : printf("The result is %d %d %d %d\n.", &num1, &num2, &res, &err);
-
- printf("The result is %d %d %d %d\n.", num1, num2, res, err);
-
- : }
- : -------------------------------
- :
- : pretty simple right?
- :
- : --------------------------------
- : Please input two number!
- : 2 2
- : You entered 2817020 and 2817016.
-
- These are the _addresses_ of the two values entered.
-
- : And now the code for the operation!
- : 1=Add, 2=Subtract, 3=Multiply, 4=Divide
- : 1
- : You entered 2817008.
-
- Same thing again.
-
- : The result is 2817020 2817016 2817012 2817004
- : ----------------------------------
-
- Ditto.
-
- : so what am i doing wrong. i'm an absolute beginner at c but not at
- : programming. the books i have don't tell me any different way to do this. im
- : compiling w/ 'gcc -noixemul -o math math.c' also pretty simple. do i need to
- : #include some more stuff?
-
- No.
-
- Hope this helps (TM).
-
- -- Mat.
-